<template>
  <div>
    <div class="card" :style="background"></div>
    <div class="give-btn" @click="receive">立即领取</div>
    <!--领取码弹框-->
    <div v-if="show" class="drawer_screen" @click="show = false"></div>
    <div v-if="show" class="drawer_box_ad">
      <div class="drawer_content_ad">
        <div style='font-size:16px;display:flex;justify-content:center;padding-top:10px;'>请输入领取码</div>
        <input
          placeholder="请输入领取码"
          v-model="receive_code"
          focus="true"
          class="num-input"/>
        <div style="display:flex;">
          <div class="dialog-btn" @click="show = false">取消</div>
          <div class="dialog-btn" style="border-left:1px solid #D7D5D5;color:#835F3F;" @click="submit()">确定</div>
        </div>
      </div>
    </div>
    <div class="home" @click="handleHome()">我的</div>
  </div>
</template>

<script>
import api from '@/utils/api'
import {
  mapState,
  mapActions,
  mapGetters
} from 'vuex'
export default {
  data() {
    return {
      show:false,
      gift_card:{},
      receive_code:''
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ]),
    background(){
      return "background-image:url('"+this.gift_card.background_pic+"')" 
    }
  },
  methods: {
    handleHome(){
      wx.switchTab({url: '/pages/user/index'})
    },
    changeCode(e){
      this.receive_code = e.mp.detail.value
    },
    receive(){
      this.show = !this.show
    },
    async submit(){
      console.log('receive_code=>',this.receive_code)
      let form = {
        receive_code:this.receive_code,
        user_id:this.user.id,
        gift_card_id:this.gift_card.id
      }
      let result = await api.receiveGiftCard(form)
      if (result.code == 0) {
        this.show = !this.show
        wx.showToast({title: '领取成功，可在优惠券查看',icon: 'none'})
        this.refreshUser()
      }
    },
    refreshUser() {
      api.getUserByOpenid({
        openid: this.user.open_id
      }).then(res => {
        if (res.code == 0) {
          this.$store.dispatch('setUser', res.data)
        }
      })
    },
  },
  async onLoad (options) {
    let id = options.gift_card_id
    let result = await api.getGiftCard({id:id})
    if(result.code == 0){
      this.gift_card = result.data
    }
  }
}
</script>

<style>
page{
  background: #F2F2F2;
}
.card{
  width: 90%;
  height: 160px;
  margin-left: 5%;
  margin-top: 30px;
  border-radius: 10px;
  overflow: hidden;
  background-repeat:no-repeat;
  background-size:100% 100%;
}
.give-btn{
  width: 80%;
  height: 40px;
  margin: 20px auto;
  font-size: 16px;
  text-align: center;
  line-height: 40px;
  background: red;
  color: white;
  border-radius: 5px;
}
.home{
  position: fixed;
  bottom: 50px;
  right: 0px;
  height: 30px;
  line-height: 30px;
  font-size: 14px;
  width: 60px;
  background: #FA5B58;
  text-align: center;
  color: #fff;
  border-top-left-radius: 15px;
  border-bottom-left-radius: 15px;
}
.dialog-title{
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  font-size: 18px;
}
.dialog-input{
  border: 1px solid black;
  width: 90%;
  margin:0 auto;
  margin-bottom: 10px;
  border-radius: 5px;
  padding: 2px;
  text-align: center;
}
.drawer_screen {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 2000;
  background: #000;
  opacity: 0.5;
  overflow: hidden;
}
.drawer_box_ad {
  margin-left: 5%;
  width: 90%;
  overflow: hidden;
  position: fixed;
  top: 20%;
  z-index: 2001;
  background: #FFFFFF;
  border-radius: 5px;
}
.drawer_content_ad {
  width: 100%;
}
.dialog-btn{
  width: 50%;
  height: 50px;
  line-height: 50px;
  border-top: 1px solid #D7D5D5;
  text-align: center;
}
.num-input{
  margin: 20px;
  padding: 5px;
  border-radius: 5px;
  border: 1px solid #D7D5D5;
}
</style>